///******************** NG.Respate(C)  **********************************
//* 产品名称: Respate 科技
//* 文件名称: signal.c
//* 程序作者: 雷帕特公司(南宫子萍)
//* 程序版本: V1.1														   
//* 编制日期: 2019/1/29
//* 功能描述: 系统日志
//* QQ:	2085827949
//**********************************************************************/

#include "flight.h"



int showMode=0 ;   //


/*********************************************************************************
** 函数名称: showeFlightInfo
** 功能描述: 显示用户信息
** 输入参数: flight: 用户结构体.
** 输出参数: None.
** 返回参数: None.
**********************************************************************************/
void showeFlightInfo(Flight* flight)
{
    //system("clear");
    isNull(flight,"showeFlightInfo Err",);
    if(!showMode){  showMode=!showMode;
    printf("**************************** 航班信息 ****************************\r\n");
    printf("    航班号  起点站  终点站      班期        机型    起飞时间    票价\r\n");}
    printf("    %s  ",flight->number);
    printf("    %s  ",flight->staddress);
    printf("    %s  ",flight->arraddress);
    printf("    %s  ",flight->date);
    printf("    %c  ",flight->type);
    printf("    %s  ",flight->stime);
    printf("    %d  ",flight->price);
    
    printf("\r\n");
}




/*********************************************************************************
** 函数名称: getFlight_byName
** 功能描述: 通过姓名 得到用户
** 输入参数: name：用户名
** 输出参数: None.
** 返回参数: 失败：NULL.
**********************************************************************************/
void showeAllFlight(FlightList* flightList)
{
    int i=0;
    int len = LinkList_Length(flightList->listHead);
    Flight* flight = NULL;
    system("clear");
    for(i=0;i<len;i++)
    {
        flight = (Flight*)LinkList_Get(flightList->listHead,i);
        if(flight!=NULL)
        {   
            showMode=i;
            showeFlightInfo(flight);
        }   
    }
    printf("******************************************************************\r\n");
} 
   


/*********************************************************************************
** 函数名称: decodeFlight
** 功能描述: 解析用户
** 输入参数: src: 用户字符串.
** 输出参数: None.
** 返回参数: .
**********************************************************************************/
Flight* decodeFlight(char* src)
{
    int i = 0;
    int offset = 0;
    int len =-1;
    Flight * flight =NULL;

    char price[10] = {0};
    char type[10] = {0};
    isNull(src,,-1);

    flight = (Flight*)calloc(sizeof(Flight),1);
    isNull(flight,"decodeFlight calloc Err",NULL);

    decodeItem(&src[offset],flight->number,',',&len);       offset+=len+1;
    decodeItem(&src[offset],flight->staddress,',',&len);    offset+=len+1;
    decodeItem(&src[offset],flight->arraddress,',',&len);   offset+=len+1;
    decodeItem(&src[offset],flight->date,',',&len);         offset+=len+1;
    decodeItem(&src[offset],type,',',&len);                 offset+=len+1;
    decodeItem(&src[offset],flight->stime,',',&len);        offset+=len+1;
    decodeItem(&src[offset],price,',',&len);

    flight->price = atoi(price);
    flight->type = type[0];
    return flight;  //返回解析后的节点
    // showeFlightInfo(flight);
    
}


int getFilghtNumber(FlightList* flightList)
{
    //printf("%d:\n",LinkList_Length(flightList->listHead));
    return LinkList_Length(flightList->listHead);
}

/*********************************************************************************
** 函数名称: decodeFlightFile
** 功能描述: 解析用户文件
** 输入参数: src: 用户文件.
** 输出参数: None.
** 返回参数: .
**********************************************************************************/
FlightList* decodeFlightFile(char* src)
{
    char* head =NULL;
    char* end =NULL;
    char* data = NULL;
    FlightList* flightList = NULL;
    Flight* flight =NULL;
    char* srcDir = NULL;

    flightList = (FlightList*) calloc(sizeof(FlightList),1);
    flightList->listHead = (LinkList*) LinkList_Create();
    isNull(flightList->listHead,"flightInit is NULL",NULL);//判断是否为空
    strcpy(flightList->srcDir,src); //赋值文件地址

    head = data = fileRead(src);
    if(head == NULL){
        //释放内存
        //printf("decodeFlight err!\r\n");
        return flightList;
    }
    while (1){
        head = strpbrk(head,"@#");
        if(head ==NULL) break;
        head+=2;
        flight =  decodeFlight(head);
        if(flight == NULL) break;
        flight->owenr = flightList;
        addFlight(flight);   //添加节点到链表
    }
    
    free(data);
    return flightList;
}

/*********************************************************************************
** 函数名称: flightSave
** 功能描述: 保存用户
** 输入参数: None.
** 输出参数: None.
** 返回参数: .
**********************************************************************************/
void flightSave(FlightList* flightList)
{
    int i=0;
    int count = 0;
    Flight* flight = NULL;
    char price[10] = {0};
    char type[2] = {0};
    char buff[1024] = {0};
    int len = LinkList_Length(flightList->listHead);
    
    isNull(flightList->listHead,"flightSave Err",);//判断是否为空
    isNull(flightList->listHead->next,"",);//判断是否为空
    
    Lap_DeletFile(flightList->srcDir);
    lap_creatFile(flightList->srcDir,NULL);    //强制创建文件，防止出错

    for(i=0;i<len;i++)
    {
        flight = (Flight*)LinkList_Get(flightList->listHead,i);
        if(flight!=NULL)
        {   
            LAP_Int2Str(flight->price,price);
            type[0] = flight->type;
            count += LAP_StrCopy_endChar("@#",&buff[count],'\0');
            count += LAP_StrCopy_endChar(flight->number,&buff[count],'\0');     buff[count++] = ',';    //分割符
            count += LAP_StrCopy_endChar(flight->staddress,&buff[count],'\0');  buff[count++] = ',';    //分割符
            count += LAP_StrCopy_endChar(flight->arraddress,&buff[count],'\0'); buff[count++] = ',';    //分割符
            count += LAP_StrCopy_endChar(flight->date,&buff[count],'\0');       buff[count++] = ',';    //分割符
            count += LAP_StrCopy_endChar(type,&buff[count],'\0');               buff[count++] = ',';    //分割符
            count += LAP_StrCopy_endChar(flight->stime,&buff[count],'\0');      buff[count++] = ',';    //分割符
            count += LAP_StrCopy_endChar(price,&buff[count],'\0');              buff[count++] = '\n';    //分割符 

            fileWrite(flightList->srcDir,buff,count);
            //printf("len:%d %s",count,buff);
            memset(buff,0,sizeof(buff));
            memset(price,0,sizeof(price));
            count =0;
        }   
    }
    
}



/*********************************************************************************
** 函数名称: addFlight
** 功能描述: 添加用户
** 输入参数: flight：用户 
** 输出参数: None.
** 返回参数: 失败：NULL.
**********************************************************************************/
int addFlight(Flight* flight)
{
    //printf("srcDir:%s\r\n",flight->number);
    return LinkList_Insert(((FlightList*)flight->owenr)->listHead,flight,0);
}



/*********************************************************************************
** 函数名称: getFlight_byName
** 功能描述: 通过姓名 得到用户
** 输入参数: name：用户名
** 输出参数: None.
** 返回参数: 失败：NULL.
**********************************************************************************/
Flight* getFlight_byName(FlightList* flightList,char* name)
{
    int i=0;
    int len = LinkList_Length(flightList->listHead);
    Flight* flight = NULL;

    for(i=0;i<len;i++)
    {
        flight = (Flight*)LinkList_Get(flightList->listHead,i);
        if(flight!=NULL)
        {
            if(strcmp(flight->number,name)==0) return flight;
        }   
    }
    
    return NULL;
}

/*********************************************************************************
** 函数名称: deleteFlight
** 功能描述: 添加用户
** 输入参数: flight：用户 
** 输出参数: None.
** 返回参数: 失败：NULL.
**********************************************************************************/
void deleteFlight(Flight* flight)
{
    Flight* temp = NULL;
    if(getFilghtNumber(flight->owenr)<=0)
    {
        printf("尚无航班\r\n");
        return;
    } 
    temp = LinkList_DeleteByNode(((FlightList*)flight->owenr)->listHead,flight);
    isNull(temp,"deleteFlight err",);
    flightSave(flight->owenr); //保存用户链表
    free(temp);
}

/*********************************************************************************
** 函数名称: deleteFlight_ByName
** 功能描述: 添加用户
** 输入参数: flight：用户 
** 输出参数: None.
** 返回参数: 失败：NULL.
**********************************************************************************/
int deleteFlight_ByName(FlightList* flightList,char* name)
{
    Flight* flight = NULL;
    flight = getFlight_byName(flightList,name);
    isNull(flight,"删除航班错误！",-1);
    deleteFlight(flight);
    return 0;
}



/*********************************************************************************
** 函数名称: registerFlight
** 功能描述: 注册用户
** 输入参数: name：用户名 passwd:密码        
** 输出参数: None.
** 返回参数: return 0:成功  其它：失败.
**********************************************************************************/
int registerFlight(FlightList* flightList)
{
    char * ret = NULL;
    Flight* flight = NULL;
    char type[2] = {0};
    char price[10] = {0};

    isNull(flightList->listHead,"registerFlight flightList Err！",-1);
    flight = (Flight*)calloc(sizeof(Flight),1);
    isNull(flight,"registerFlight Err！",-1);

    getScanfClear("航班:",flight->number,10);
    if(getFlight_byName(flightList,flight->number)!=NULL){
         free(flight);
         isNull(NULL,"航班已存在！",-1);
    }
    getScanfClear("起点:",flight->staddress,10);
    getScanfClear("终点:",flight->arraddress,10);
    getScanfClear("班期:",flight->date,15);
    getScanfClear("机型:",type,2);
    getScanfClear("时间:",flight->stime,10);
    getScanfClear("票价:",price,10);
    flight->type = type[0];
    flight->price = atoi(price);
    flight->owenr = flightList;
    addFlight(flight);
    flightSave(flight->owenr); //保存用户链表
    isShowFlightHeadInfo(1);
    showeFlightInfo(flight);
    return 0;

}

#if 0
/*********************************************************************************
** 函数名称: logFlight
** 功能描述: 用户登录
** 输入参数: name：用户名 passwd:密码        
** 输出参数: None.
** 返回参数: return 0:成功  其它：失败.
**********************************************************************************/
int logFlight(void)
{
    char name[CFG_FLY_NAME_SIZE]={0};      //用户名
    char passwd[CFG_FLY_PASSWD_SIZE]={0};  //登录密码
    char * ret = NULL;
    Flight* flight = NULL;

    getScanfClear("用户名:",name,CFG_FLY_NAME_SIZE);
    getPasswd("密码:",passwd,CFG_FLY_PASSWD_SIZE);
    printf("用户：%s,密码：%s\r\n",name,passwd);

    flight = getFlight_byName(name);
        isNull(flight,"用户不存在！",-1);

    if(strcmp(flight->passwd,passwd)!=0) 
        isNull(NULL,"密码错误",-1);

    currentFlight = flight;
   return 0;
}
#endif

/*********************************************************************************
** 函数名称: flightInit
** 功能描述: 用户链表初始化
** 输入参数: None.
** 输出参数: None.
** 返回参数: .
**********************************************************************************/
FlightList* flightInit(char* src)
{

    return decodeFlightFile(src);
}



																						 
// void flyTest(void)
// {
//     flightInit();
//     registerFlight();
//      flightSave(flight->owenr); //保存用户链表
// }





																						 

